﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace TBM.Tools
{
    /// <summary>
    /// 分页导航类
    /// </summary>
    public class PageNavigationBar:PageNavigation
    {
        /// <summary>
        /// 初始化导航条
        /// </summary>
        /// <param name="paramName">受参名</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="count">记录总数</param>
        /// <param name="displayPages">显示分页数</param>
        public PageNavigationBar(string paramName, int pageSize, int count, int displayPages)
        {
            base.Init(paramName, pageSize, count, displayPages);
        }
        public PageNavigationBar(string paramName, int pageSize, int displayPages)
        {
            base.Init(paramName, pageSize, 0, displayPages);
        }
        /// <summary>
        /// 创建导航条
        /// </summary>
        /// <returns></returns>
        public string Create()
        {
            TextTemplate = TextTemplate.Replace(@"{count}", Count.ToString());
            TextTemplate = TextTemplate.Replace(@"{currpage}", _currPage.ToString());
            TextTemplate = TextTemplate.Replace(@"{pagesize}", PageSize.ToString());

            StringBuilder textLabel = new StringBuilder("<div class=\"col-md-5 col-sm-12\">");

            if (TextDisplay)
            {
                textLabel.AppendLine(
                    String.Format(
                        "<div class=\"dataTables_info {0}\" role=\"status\" aria-live=\"polite\">",
                        TextAlign == NavAlign.Left ? "text-left" : "text-right"
                    )
                );
                textLabel.AppendLine(TextTemplate);
                textLabel.AppendLine("</div>");
            }

            textLabel.AppendLine("</div>");

            StringBuilder pageNav = new StringBuilder("<div class=\"col-md-7 col-sm-12\">");
            pageNav.AppendLine(
                String.Format(
                    "<div class=\"dataTables_paginate paging_bootstrap_full_number{0}\">",
                    TextAlign == NavAlign.Left ? "" : " pull-left"
                )
            );
            pageNav.AppendLine("<ul class=\"pagination\" style=\"visibility: visible;\">");
            pageNav.AppendLine(
                String.Format(
                    "<li class=\"prev{0}\"><a href=\"{1}\" title=\"首页\"><i class=\"fa fa-angle-double-left\"></i></a></li>" +
                    "<li class=\"prev{0}\"><a href=\"{2}\" title=\"上一页\"><i class=\"fa fa-angle-left\"></i></a></li>",
                    _currPage == 1 ? " disabled" : "",
                    _currPage == 1 ? "javascript:void(0);" : CURL.GetURLByAddParam(QueryString, ParamName, "1"),
                    _currPage < 2 ? "javascript:void(0);" : CURL.GetURLByAddParam(QueryString, ParamName, (_currPage - 1).ToString())
                )
            );

            if (DisplayPages > 0)
            {
                int pageNum = _currPage - DisplayPages / 2 - ((DisplayPages + 1) % 2);
                int offset = PageCount - pageNum + 1;

                if (offset < DisplayPages)
                {
                    pageNum -= DisplayPages - offset;
                }

                if (pageNum < 1) pageNum = 1;

                for (int i = 0; i < DisplayPages; i++)
                {
                    if (pageNum > PageCount) break;

                    pageNav.AppendLine(
                        String.Format(
                            "<li{0}><a href=\"{1}\">{2}</a></li>",
                            pageNum == _currPage ? " class=\"active\"" : "",
                            CURL.GetURLByAddParam(QueryString, ParamName, pageNum.ToString()),
                            pageNum.ToString()
                        )
                    );

                    pageNum++;
                }
            }

            pageNav.AppendLine(
                String.Format(
                    "<li class=\"next{0}\"><a href=\"{1}\" title=\"下一页\"><i class=\"fa fa-angle-right\"></i></a></li>" +
                    "<li class=\"next{0}\"><a href=\"{2}\" title=\"尾页\"><i class=\"fa fa-angle-double-right\"></i></a></li>",
                    _currPage < PageCount ? "" : " disabled",
                    _currPage < PageCount ? CURL.GetURLByAddParam(QueryString, ParamName, (_currPage + 1).ToString()) : "javascript:void(0);",
                    _currPage >= PageCount ? "javascript:void(0);" : CURL.GetURLByAddParam(QueryString, ParamName, PageCount.ToString())
                )
            );

            pageNav.AppendLine("</ul></div></div>");

            StringBuilder nav = new StringBuilder("<div class=\"row\">");

            if (TextAlign == NavAlign.Left)
            {
                nav.AppendLine(textLabel.ToString());
                nav.AppendLine(pageNav.ToString());
            }
            else
            {
                nav.AppendLine(pageNav.ToString());
                nav.AppendLine(textLabel.ToString());
            }

            nav.AppendLine("</div>");

            return nav.ToString();
        }
    }
}
